<?php
/**
 * Conversion of quantity with different unit. Test program for answer_unit.php
 * See http://code.google.com/p/moodle-coordinate-question/ for more documentation.
 * 
 * @copyright Copyright (c) 2010, Hon Wai, Lau. All rights reserved.
 * @author Hon Wai, Lau <lau65536@gmail.com>
 * @license New and Simplified BSD licenses, http://www.opensource.org/licenses/bsd-license.php
 */

require_once('coordinates/answer_unit.php');
require_once('coordinates/conversion_rules.php');
$unit = new answer_unit_conversion();

$basic_unit_conversion_rules[999] = array($basic_unit_conversion_rules[1][0].' + Example',$basic_unit_conversion_rules[1][1].';
1 J = 1.602 eV;
1024 B = 1 KiB;
1073741824 KiB = 1048576 MiB = 1024 GiB = 1 TiB;
');


function print_dimension_list($unit) {
    $dimension_list = $unit->get_dimension_list();
    echo '<div>';
    foreach ($dimension_list as $dimension_class => $units) {
        echo '<div class="dimension_list"><table><tr><td><b>Unit</b></td>';
        foreach ($units as $unit => $scale)  echo '<td title='.$unit.'>'.$unit.'</td>';
        echo '</tr><tr><td><b>Scale</b></td>';
        foreach ($units as $unit => $scale) {
            if ($scale > 0.1 && $scale < 1000)  $s = sprintf("%1.2f", $scale);
            else $s = sprintf("%.2e",$scale);
            echo '<td title='.$scale.'>'.$s.'</td>';
        }
        echo '</tr></table></div>';
    }
    echo '</div>';
}

function convert_input($unit, $inputs, $targets) {
    $ipunits_array = explode(',', $inputs);
    $targets_array = explode('=', $targets);
    echo '<div><table><tr><td><b>Convertible</b></td><td><b>Original</b></td><td><b>Converted</b></td>
        <td><b>Conversion factor</b></td><td><b>Match target location</b></td></tr>';
    foreach ($ipunits_array as $ip) {
        echo '<tr>';
        try {
            $a = $unit->split_number_unit($ip);
            $res = $unit->check_convertibility($a->unit, $targets);
            if ($res->convertible) {
                $b = $unit->split_number_unit($targets_array[$res->target]);
                echo '<td>YES ✔</td><td>'.$a->number.' '.$a->unit.'</td>';
                echo '<td>'.$a->number*$res->cfactor.' '.$b->unit.'</td>';
                echo '<td>'.$res->cfactor.'</td><td>'.$res->target.'</td>';
            }
            else
                echo '<td>NO ✗</td><td>'.$a->number.' '.$a->unit.'</td><td></td><td></td><td></td>';
        } catch(Exception $e) { echo $e->getMessage(); }
        echo '</tr>';
    }
    echo '</tr></table>';
}

$inputs = '';
$targets = '';
$ruleid = 0;
$otherrule = '';
if (isset($_REQUEST)) {
    $inputs = $_REQUEST['inputs'];
    $targets = $_REQUEST['targets'];
    $ruleid = $_REQUEST['ruleid'];
    $otherrule = $_REQUEST['otherrule'];
}
?>



<style type="text/css" media="screen">
.dimension_list { border-spacing: 0px; padding : 2px; display: block; }
table { border: 1px solid grey; border-spacing: 1px; padding : 0px; background-color: grey}
td { border: 0px; padding: 4px; min-width: 80px; background-color: #EEFFEE; text-align: right; vertical-align:text-top}
.hint { color: grey }
</style>


<h1>Check whether an input is convertible to target units</h1>
<form action='convert_unit.php' method="post">
<table>
    <tr>
        <td>Input quantity</td><td style="text-align: left">
        <input type='text' size='80' name='inputs'<?php echo " value='$inputs'"; ?>>
        <div class='hint'>e.g. 1 J, N m, 10 kg m^2 s^(-2), 4.2 kJ, 1.1 mJ, N mm, cm^2 mg/s^2, eV, keV, MN m, N</div></td>
    </tr>
    <tr>
        <td>Target units</td><td style="text-align: left">
        <input type='text' size='80' name='targets'<?php echo " value='$targets'"; ?>>
        <div class='hint'>e.g. J = N m = kg m^2/s^2</div></td>
    </tr>
    <tr>
        <td>Basic rule</td><td style="text-align: left"><select name="ruleid">
        <?php
            foreach ($basic_unit_conversion_rules as $id => $entry)
                echo '<option value="'.$id.'"'.($id == $ruleid ? ' selected="selected"' : '').'>'.$entry[0].'</option>'
        ?>
        </select>
        <div class='hint'>A predefined rule can be chosen.</div></td>
    </tr>
    <tr>
        <td>Other rule</td><td style="text-align: left">
        <textarea name="otherrule" cols="80" rows="1"><?php echo $otherrule; ?></textarea>
        <div class='hint'>e.g. "1 J = 1.6e19 eV; eV: k M G T" (Link eV to the same dimension as J, note the order)</div></td>
    </tr>
</table>
<input type='submit' value='Submit'>
</form>


<?php
if (count($_REQUEST) == 0)  exit();     // don't compute anything for the first time
try {
    try {
        $unit->assign_default_rules($ruleid, $basic_unit_conversion_rules[$ruleid][1]);
        $unit->reparse_all_rules();
    } catch(Exception $e) { echo $e->getMessage(); exit();}

    try {
        $unit->assign_additional_rules($otherrule);
        $unit->reparse_all_rules();
    } catch(Exception $e) { echo $e->getMessage(); exit();}

    echo '<h2>Conversion result</h2>';
    echo 'Inputs: '.$inputs.'<br>';
    echo 'Targets: '.$targets.'<br>';
    echo 'Basic rule: '.$basic_unit_conversion_rules[$ruleid][0].'<br>';
    echo 'Other rule: '.$otherrule.'<br>';
    convert_input($unit, $inputs, $targets);

    echo '<h2>Dimension class table (Basic rule + Other rule)</h2>';
    print_dimension_list($unit);

    echo '<h2>Basic rule (unparsed)</h2>';
    echo implode(',<br>',explode(',',$basic_unit_conversion_rules[$ruleid][1]));
} catch (Exception $e) {
    echo $e->getMessage();
}
?>
